package com.jnshu.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jnshu.dao.RoleMapper;
import com.jnshu.dto.admin.RoleDto;
import com.jnshu.pojo.Role;
import com.jnshu.service.RoleService;
import com.jnshu.tools.binding.JsonUtil;
import com.jnshu.tools.resultUtils.PageBean;
import com.jnshu.tools.resultUtils.RUtil;
import com.jnshu.tools.resultUtils.ResultBean;
import com.jnshu.tools.resultUtils.ResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Martin
 * @date 2019/8/12 21:24
 */
@Slf4j
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    RoleMapper roleMapper;

    @Override
    public PageBean selectRoleList(int pageNum, int pageSize) {
        Page page = PageHelper.startPage(pageNum,pageSize);
        List<Role> roleList = roleMapper.selectRoleList();
        log.info("RoleServiceImpl查询出来的角色列表为："+ JsonUtil.toJsonString(roleList));
        if (roleList == null){
            log.info("RoleServiceImpl的selectRoleList查询数据为空");
            return RUtil.okDefault(0,null,pageNum,pageSize);
        }
        return RUtil.okDefault(page.getTotal(),roleList,pageNum,pageSize);
    }

    @Override
    public ResultBean selectRoleById(long id) {
        log.info("RoleServiceImpl查询的角色id为："+id);
        Role role = roleMapper.selectByRoleId(id);
        log.info("RoleServiceImpl查询出来的角色信息为："+role);
        if (role == null){
            log.info("RoleServiceImpl的selectRoleById查询数据为空");
            return RUtil.error(ResultEnum.FAILURE);
        }
        return RUtil.ok(role);
    }

    @Override
    public ResultBean deleteByPrimaryKey(Long id) {
        log.info("RoleServiceImpl删除的角色id为："+id);
        int result = roleMapper.deleteByRoleId(id);
        log.info("RoleServiceImpl的删除结果为："+result);
        if (result == 1){
            return RUtil.ok();
        }else {
            return RUtil.error(ResultEnum.NOTHINGNESS);
        }
    }

    @Override
    public ResultBean insertSelective(RoleDto roleDto,String loginId) {
        Long id = Long.valueOf(loginId);
        Role role = roleMapper.selectByRoleName(roleDto.getRoleName());
        if (role != null){
            log.info("RoleServiceImpl查出来的role不为空，已有角色："+role);
            return RUtil.error(ResultEnum.SAME_NAME);
        }
        Role newRole = new Role();
        newRole.setName(roleDto.getRoleName());
        newRole.setPermission(roleDto.getPermission());
        newRole.setCreateAt(System.currentTimeMillis());
        newRole.setUpdateAt(System.currentTimeMillis());
        newRole.setCreateBy(Long.valueOf(loginId));
        newRole.setUpdateBy(Long.valueOf(loginId));
        log.info("newRole对象中的参数为："+JsonUtil.toJsonString(newRole));
        if (roleMapper.insertBySelective(newRole) == 1){
            log.info("RoleServiceImpl查出来的role为空,添加新角色");
            return RUtil.ok();
        }
        return RUtil.error(ResultEnum.ERROR_ADD);
    }

    @Override
    public ResultBean updateByPrimaryKeySelective(RoleDto roleDto,String loginId) {
        log.info("updateByPrimaryKeySelective的参数信息roleDto为："+JsonUtil.toJsonString(roleDto));
        Role role = roleMapper.selectByRoleId(roleDto.getRoleId());
        log.info("updateByPrimaryKeySelective的权限id为："+roleDto.getRoleId());
        log.info("ManageServiceImpl根据传入数据id查询出要修改的role："+role);
        if (role != null){
            if (roleDto.getRoleName().equals("")){
                log.info("roleDto的roleName属性值："+roleDto.getRoleName());
                return RUtil.error(ResultEnum.SAME_NAME);
            }
            role.setName(roleDto.getRoleName());
            role.setPermission(roleDto.getPermission());
            role.setUpdateAt(System.currentTimeMillis());
            role.setUpdateBy(Long.valueOf(loginId));
            if (roleMapper.updateBySelective(role) == 1){
                return RUtil.ok();
            }
        }
        return RUtil.error(ResultEnum.ERROR_NULL_MANAGE);
    }


}
